<!-- 农机类型管理 - 添加/修改 -->
<template>
  <div class="dialog">
    <el-dialog :title="'农机'+titleText" :visible.sync="showDialog" width="55%" top="0" :close-on-click-modal="false" @close="cancel">

      <el-form ref="ruleForm" label-position="left" :model="formData" :rules="rules" class="el-form">
        <el-form-item label="农机类型名称:" class="w50p" prop="type">
          <el-input v-model="formData.type" clearable placeholder="请输入农机类型名称"/>
        </el-form-item>
        <el-form-item label="农机总数:" class="w50p" prop="totalNum">
          <el-input v-model="formData.totalNum" clearable placeholder="请输入农机总数"  type="number"/>
        </el-form-item>
        <el-form-item label="使用中数量:" class="w50p" prop="inUseNum">
          <el-input v-model="formData.inUseNum" clearable placeholder="请输入使用中数量"  type="number"/>
        </el-form-item>
        <el-form-item label="维保数量:" class="w50p" prop="maintenanceNum">
          <el-input v-model="formData.maintenanceNum" clearable placeholder="请输入维保数量"  type="number"/>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button class="cancelbtn" @click="cancel()">取 消</el-button>
        <el-button type="primary" class="savebtn" @click="save('ruleForm')">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import BreedingTypeApi from "@/api/basic-info-manage/breeding-type-manage/index";
import AgriculturalMachineType from "@/api/basic-info-manage/agriculturalMachine-type-manage/index";  // 农机类型管理
import EnumApi from "@/api/enum";

export default {
  name: "",
  components: {},
  props: {
    // 弹框名称
    titleText: {
      type: String,
      default: "",
    },
    nowChanceStau: {
      type: String,
      default: "",
    },
    show: {
      type: Boolean,
      default: false,
    },
    editObject: {
      type: [Object],
      default: () => {
        return {};
      },
    },
  },
  data() {
    // 农机总数
    let totalNumCheck = (rule, value, callback) => {
      if (value<=0) {
        return callback(new Error('农机总数要大于0'));
      }else if(value%1!==0){
        return callback(new Error('请输入整数'))
      }else{
        return callback()
      }
    }
    // 使用中数量
    let inUseNumCheck = (rule, value, callback) => {
      if (value<0) {
        return callback(new Error('使用中数量>=0'));
      }else if(value%1!==0){
        return callback(new Error('请输入整数'))
      }else{
        return callback()
      }
    }
    // 维保数量
    let maintenanceNumCheck = (rule, value, callback) => {
      if (value<0) {
        return callback(new Error('维保数量>=0'));
      }else if(value%1!==0){
        return callback(new Error('请输入整数'))
      }else{
        return callback()
      }
    }
    return {
      formData: {},
      BreedingTypeList: {},
      rules: {
        type: [
          { required: true,message: "请输入农机类型名字",trigger: ["change", "blur"],},
          // { validator: typeCheck, trigger: ["change", "blur"] }
        ],
        totalNum:[
          { required: true,message: "请输农机总数量",trigger: ["change", "blur"],},
          { validator: totalNumCheck, trigger: ["change", "blur"] }
        ],
        inUseNum:[
          { required: true,message: "请输入使用中数量",trigger: ["change", "blur"],},
          { validator: inUseNumCheck, trigger: ["change", "blur"] }
        ],
        maintenanceNum:[
          { required: true,message: "请输入维保数量",trigger: ["change", "blur"],},
          { validator: maintenanceNumCheck, trigger: ["change", "blur"] }
        ],
      },
      showDialog: this.show,
    };
  },
  computed: {},
  watch: { 
    show(v) {
      this.showDialog = v;
    },
    nowChanceStau(v) {
      if (v === "add") {
        this.formData = {
          staffName: sessionStorage.getItem('staffName'),
        };
        this.$nextTick(() => {
          this.$refs["ruleForm"].clearValidate();
        });
      }
      if (v === "edit") {
        this.formData = this.editObject;
      }
    },
  },
  created() {},
  mounted() {
    // this.queryEnum()
  },
  methods: {
    // async queryDiseaseTpye() {
    //   const res = await AgriculturalMachineType.queryByCondition({});
    //   this.BreedingTypeList = res.data;
    // },
    save(formData) {
      this.$refs[formData].validate((valid) => {
        if (valid) {
          if (this.nowChanceStau === "edit") {
            AgriculturalMachineType.edit(this.formData).then((res) => {
              if (res.success === true) {
                this.$message.success("修改成功！");
                this.close();
              } else {
                this.$message.error(res.message);
              }
            });
          } else if (this.nowChanceStau === "add") {
            AgriculturalMachineType.add(this.formData).then((res) => {
              console.log('添加',res);

              if (res.success === true) {
                this.$message.success("添加成功！");
                this.close();
              } else {
                this.$message.error(res.message);
              }
            });
          }
        } else {
          return false;
        }
      });
    },
    close() {
      this.$emit("closeDialog");
    },
    cancel() {
      this.$emit("cancelDialog");
    },
  },
};
</script>

<style lang="scss" scoped>
</style>
